﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ezCloud.Api.Common;
using ezCloud.Data;
using Nest;
using ezCloud.Api.Models;
using System.Configuration;
using System.Threading.Tasks;

namespace ezCloud.Api.Controllers
{
    [RoutePrefix("api/Search")]
    [ApiAuthorize]
    public class SearchController : ezController

    {
        public static string defaultIndex = ConfigurationSettings.AppSettings["elasticIndex"];
        [Route("Get")]
        public async Task<IHttpActionResult> GetSearchResult(string query)
        {
            /*query = query.ToLower();
            
            var currentUser = Common.UserUtitily.getUserProfile();
            var reservations = context.ReservationRooms.Where(rr => rr.HotelId == currentUser.HotelId && (rr.BookingStatus == "CHECKIN" || rr.BookingStatus == "BOOKED") && (rr.Travellers!= null && rr.Travellers.Fullname != null && rr.Travellers.Fullname.ToLower().Contains(query))).Select(rr => new {Name = rr.Travellers.Fullname, RoomId = rr.RoomId, Id = rr.ReservationRoomId, Status = rr.BookingStatus }).ToList();
            var rooms = context.Rooms.Where(room => room.HotelId == currentUser.HotelId && (room.RoomName.ToLower().Contains(query) || room.RoomDescription.ToLower().Contains(query))).ToList();
            
            return Ok(new {
                Reservations = reservations,
                Rooms = rooms
            });*/
            
        var user = Common.UserUtitily.getUserProfile();
            var node = new Uri("http://localhost:9200");
            var settings = new ConnectionSettings(
                    node,
                    defaultIndex: defaultIndex
                );
            var client = new ElasticClient(settings);
            var result = client.Search<Object>(s=>s.Index(defaultIndex).Types("reservationroom").Query(q => q.Match(m => m.OnField("_all").Query(query))).SortDescending("reservation.reservationRoomId").Filter(f => f.Term("reservation.hotelId", user.HotelId)));
            return Ok(new { Reservations = result.Documents, Highlight = result.Highlights});
        }
        [Route("GetRecent")]
        public async Task<IHttpActionResult> GetRecent()
        {
            var user = Common.UserUtitily.getUserProfile();
            var node = new Uri("http://localhost:9200");
            var settings = new ConnectionSettings(
                    node,
                    defaultIndex: defaultIndex
                );
            var client = new ElasticClient(settings);
            var result = client.Search<RecentSearchModel>(s => s.Index(defaultIndex)
                .Types("recentsearch")
                .Query(qq => 
                    qq.Filtered(fd => fd.Query(q => q.MatchAll()).Filter(f => f.Prefix("_id",user.HotelId + "." + user.UserId + ".")))
                ).SortDescending("datetime"));
            return Ok(result.Documents);
        }
        [Route("SaveRecent")]
        public async Task<IHttpActionResult> SaveRecentSearch(RecentSearchModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var user = Common.UserUtitily.getUserProfile();
            model.user = user;
            model.id = model.user.HotelId + "." + model.user.UserId + "." + model.path;
            model.datetime = DateTime.UtcNow.Ticks;
            Common.Elastic.IndexRecentSearch(model);

            return Ok();
        }
    }
}
